<?php

namespace App\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SellPackageContentModel extends Model
{
    use HasFactory;

    protected $table = 'sell_package_content';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getSellPackageContent($mainId, $condition)
    {
        return SELF::select(
            'sell_id'
        )
            ->from('sell_package_content as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.materiel_type_id')
            ->distinct()
            ->where($mainId)
            ->where($condition)
            ->get()->toArray();
    }

    public function packageBatchInfo()
    {
        return SELF::hasMany(SellPackageBatchModel::class, 'sell_package_content_id', 'id');
    }

    static public function getBatchInfo($sellId, $data)
    {
        return SELF::select(
            'materiel_type_id'
        )
            ->from('sell_package_content as a')
            ->leftJoin('sell_package_batch as b', 'a.id', '=', 'b.sell_package_content_id')
            ->where('a.sell_id', $sellId)
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }
}
